package org.shiki.hrm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class HrmAuthenConfig extends WebSecurityConfigurerAdapter {
	/**
	 * 设置密码编码器
	 * @return
	 */
	@Bean
	public PasswordEncoder getEncoder(){
		return new BCryptPasswordEncoder();
//	return NoOpPasswordEncoder.getInstance();
	}

	/**
	 * http安全配置
	 * @param http
	 * @throws Exception
	 */
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
				.antMatchers(HttpMethod.POST, "/login","/login.html","/hrm/login", "/hrm/rtoken").permitAll()
				.anyRequest().authenticated()
				.and().formLogin().permitAll()
//				.loginProcessingUrl("/success")
				.and().logout().permitAll()
				.and().csrf().disable();
	}

	@Override
	@Bean
	protected AuthenticationManager authenticationManager() throws Exception {
		return super.authenticationManager();
	}
}
